package com.liu.audio.asr;

import com.alibaba.dashscope.audio.asr.recognition.Recognition;
import com.alibaba.dashscope.audio.asr.recognition.RecognitionParam;
import com.alibaba.dashscope.audio.asr.vocabulary.Vocabulary;
import com.alibaba.dashscope.audio.asr.vocabulary.VocabularyService;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class VocabularySampleCodes {
    public static String apiKey = "sk-6d51bedf84164e0087efbc2f52dec673";

    public static void main(String[] args) throws NoApiKeyException, InputRequiredException {
        /**
         * 支持的模型列表
         * 实时语音识别：paraformer-realtime-v2，paraformer-realtime-8k-v2
         * 录音文件识别：paraformer-v2，paraformer-8k-v2
         */
        String targetModel = "paraformer-realtime-v2";

         updateVocabulary();
//        Vocabulary myVoc = createVocabulary(targetModel);

//        String result = getString(targetModel, myVoc);
//        System.out.println(result);
        System.exit(0);
    }

    private static String getString(String targetModel, Vocabulary myVoc) {
        // use the vocabulary to recognize a file
        Recognition recognizer = new Recognition();
        RecognitionParam param = RecognitionParam.builder()
                        .model(targetModel)
                        .format("wav")
                        .sampleRate(16000)
                        .apiKey(apiKey)
                        .vocabularyId(myVoc.getVocabularyId())
                        // “language_hints”只支持paraformer-v2和paraformer-realtime-v2模型
                        .parameter("language_hints", new String[] {"zh"})
                        .build();
        return recognizer.call(param, new File("your-local-audio-file.wav"));
    }

    private static void  updateVocabulary() throws NoApiKeyException, InputRequiredException {
        JsonArray vocabulary = new JsonArray();
        List<HotWord> wordList = new ArrayList<>();
        wordList.add(new HotWord("钻井优化", 2, "zh"));

        for (HotWord word : wordList) {
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("text", word.text);
            jsonObject.addProperty("weight", word.weight);
            jsonObject.addProperty("lang", word.lang);
            vocabulary.add(jsonObject);
        }

        VocabularyService service = new VocabularyService(apiKey);
        // create vocabulary
        service.updateVocabulary("vocab-Drilling-3a1606ab71e449299e809f064a6440cc",vocabulary);
    }
    private static @NotNull Vocabulary createVocabulary(String targetModel) throws NoApiKeyException, InputRequiredException {
        JsonArray vocabulary = new JsonArray();
        List<HotWord> wordList = new ArrayList<>();
        wordList.add(new HotWord("钻井优化", 5, "zh"));
        wordList.add(new HotWord("地质导向", 5, "zh"));

        for (HotWord word : wordList) {
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("text", word.text);
            jsonObject.addProperty("weight", word.weight);
            jsonObject.addProperty("lang", word.lang);
            vocabulary.add(jsonObject);
        }

        VocabularyService service = new VocabularyService(apiKey);

        // create vocabulary
        Vocabulary myVoc = service.createVocabulary(targetModel, "Drilling", vocabulary);
        System.out.println("your vocabulary id is " + myVoc.getVocabularyId());
        return myVoc;
    }

    // prepare vocabulary
    static class HotWord {
        /**
         * 热词文本，每个词语最长10个汉字或英文单词。
         */
        String text;
        /**
         * 热词权重，取值范围为[1, 5]之间的整数。 常用值：4。
         * 如果效果不明显可以适当增加权重，但是当权重较大时可能会引起负面效果，导致其他词语识别不准确。
         */
        int weight;
        /**
         * 语言代码，支持对ASR模型对应的语种做热词加强。具体语种和语言代码对应关系请参考模型的API详情页。
         * 请在调用语音识别服务使用指定同样的语种，如果指定识别语种language_hints后其他语种热词会失效。
         */
        String lang;

        public HotWord(String text, int weight, String lang) {
            this.text = text;
            this.weight = weight;
            this.lang = lang;
        }
    }
}